package com.example.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.R;
import com.example.system.entity.Article;
import com.example.system.mapper.ArticleMapper;
import com.example.system.service.IArticleService;
import com.example.system.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 王鹤蒲
 * @since 2023-08-29
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    private Logger logger= LoggerFactory.getLogger(ArticleServiceImpl.class);

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private IUserService iUserService;

    @Override
    public R saveArticle(Article article) {
        LocalDateTime pubdate = LocalDateTime.now();
        article.setPublisher(iUserService.getCurrentLoginUser().getUserName()).setPubdate(pubdate);
        articleMapper.insert(article);
        return R.success();
    }

    @Override
    public R updateArticle(Integer id,Article article) {
        LocalDateTime updateTime=LocalDateTime.now();
        LambdaUpdateWrapper<Article> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Article::getId,id);
        lambdaUpdateWrapper.set(Article::getTitle,article.getTitle());
        lambdaUpdateWrapper.set(Article::getContent,article.getContent());
        lambdaUpdateWrapper.set(Article::getUpdateBy,iUserService.getCurrentLoginUser().getUserName());
        lambdaUpdateWrapper.set(Article::getUpdateTime,updateTime);
        articleMapper.update(new Article(),lambdaUpdateWrapper);
        return R.success();
    }

    @Override
    public R selectAll(String title, int currentPage, int pageSize) {
        Page<Article> page=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Article> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Article::getTitle,title);
        lambdaQueryWrapper.eq(Article::getDelFlag,"0");
        return R.success(articleMapper.selectPage(page,lambdaQueryWrapper));
    }

    @Override
    public R queryArticleDetail(Integer id) {
        LambdaQueryWrapper<Article> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Article::getId,id);
        return R.success(articleMapper.selectOne(lambdaQueryWrapper));
    }

    @Override
    public R deleteById(Integer id) {
        LocalDateTime deleteTime=LocalDateTime.now();
        articleMapper.updateById(new Article().setId(id).setUpdateBy(iUserService.getCurrentLoginUser().getUserName()).setUpdateTime(deleteTime).setDelFlag("1"));
        return R.success();
    }

    @Override
    public R deleteByIds(Integer[] ids) {
        LocalDateTime deleteTime=LocalDateTime.now();
        for(Integer id:ids){
            articleMapper.updateById(new Article().setId(id).setUpdateBy(iUserService.getCurrentLoginUser().getUserName()).setUpdateTime(deleteTime).setDelFlag("1"));
        }
        return R.success();
    }


}
